import { Component, Host } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppComponent } from '../../../../app.component';
import { Subscription } from 'rxjs/Subscription';
import { CompanyRiskRatingService } from './companyRiskRating.service';
import { environment } from '../../../../../environments/environment';
import { CompanyService } from '../company.service';
import { CompanyModel } from '../company.model';
import { RiskCompanyModel } from './models/riskCompany.model';
import { CompanyReportDownloadModel } from '../shared/companyHeader/companyReportDownload/companyReportDownload.model';
import { ReportAnalytics } from '../../../../shared/models/googleAnalytics.model';
import { ResearchState } from '../../research.service';
import { ClusterService } from '../../riskCluster/cluster.service';

/**
 * Company risk rating module
 *
 * Author: Sorin Spiru
 * Date: 16.01.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'companyRiskRating.component.html',
    styleUrls: ['./companyRiskRating.component.scss'],
    providers: [CompanyRiskRatingService, ClusterService]
})
export class CompanyRiskRatingComponent {
    private sub: Subscription;

    private menuItems: any[];

    public company: CompanyModel;
    public riskCompany: RiskCompanyModel;
    public researchStatusText: string;
    public downloadData: CompanyReportDownloadModel;
    public analyticsData: ReportAnalytics = new ReportAnalytics('ESG Risk Rating', 'ESG RR Download PDF');
    public summaryAnalyticsData: ReportAnalytics = new ReportAnalytics('ESG Risk Rating', 'ESG RR Download Summary PDF');
    public isCore = false;


    public readonly resourcesUrl: string = environment.resourcesUrl;

    constructor(
        private route: ActivatedRoute,
        public companyService: CompanyService,
        public riskService: CompanyRiskRatingService,
        public clusterService: ClusterService,
        @Host() private rootComponent: AppComponent) {
    }

    ngOnInit() {
        this.sub = this.companyService.company.subscribe(data => {
            this.company = data;
            this.isCore = this.company.coreRiskResearchState === ResearchState.ResearchAvailable;

            //get company risk
            this.riskService.company = this.riskService.getCompanyById(this.company.id);
            if(this.clusterService.company.isEmpty) this.clusterService.getCompanyRiskClusters();
            this.clusterService.getMeiRiskClusters();
            this.clusterService.getMeiManagementRiskClusters();
            this.clusterService.getMeiExposureRiskClusters();
            this.riskService.company.subscribe(data => {
                this.riskCompany = data;
                this.researchStatusText = this.riskService.researchStatusText(this.riskCompany.availability);

                // set download report data
                this.downloadData = this.companyService.getRiskRatingDownloadData(this.company);

                if(this.riskCompany.availability == 1 && !this.isCore)
                {
                    //create menu items for baseline
                    this.menuItems = [{
                        title: this.riskCompany.issues.baseline.name,
                        path: ['risk', 'mei', this.riskCompany.issues.baseline.externalId, this.riskCompany.issues.baseline.type]
                    }];

                    //create menu items for other MEIs
                    this.riskCompany.issues.others.forEach(element => {
                        this.menuItems.push({
                            title: element.name,
                            path: ['risk', 'mei', element.externalId, element.type]
                        });
                    });

                    //update routes data
                    let meiRoute = this.route.routeConfig.children.find(x => x.path == 'mei/:externalId/:type');
                    meiRoute.data.menuItems = this.menuItems;

                    //notify the companyMenu component to display sub menu items
                    this.companyService.menuItems.next(this.route.routeConfig.children);
                    meiRoute.data.menuItems = [];
                }
                else
                {
                    //no sub menu items should be displayed except summary
                    this.companyService.menuItems.next(this.route.routeConfig.children);
                    this.rootComponent.finishLoading();
                }
            });
        });
    }

    ngOnDestroy() {
        this.sub.unsubscribe();
    }
}